//
// Created by imagination on 4/1/25.
//
#include "setting_basic_page.h"

#include "application.h"
#include "lv_i18n/lv_i18n.h"

static void screen_clibration_cb(lv_event_t * event) {
    LV_LOG_USER("screen_clibration_cb");
}

static void restore_factory_settings_cb(lv_event_t * event) {
    LV_LOG_USER("restore_factory_settings_cb");
}

static void font_changed_cb(lv_event_t * event) {
    lv_obj_t * dropdown = lv_event_get_target(event);
    uint32_t index = lv_dropdown_get_selected(dropdown);
    if (index != app.settings.font_index) {
        app.settings.font_index = index;
#if LV_USE_THEME_DEFAULT
        lv_theme_t * theme = lv_theme_default_init(NULL, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED), LV_THEME_DEFAULT_DARK,
                              app.support_fonts[app.settings.font_index].font);
        lv_display_t * display = lv_display_get_default();
        if (display) {
            lv_display_set_theme(display, theme);
        }
#endif
    }
}

static void language_changed_cb(lv_event_t * event) {
    lv_obj_t * dropdown = lv_event_get_target(event);
    uint32_t index = lv_dropdown_get_selected(dropdown);
    if (index != app.settings.language_index) {
        app.settings.language_index = index;
        lv_i18n_set_locale(app.support_language[app.settings.language_index].locale_name);
        lv_fragment_manager_send_event(app.manager, 0, NULL);
    }
}

static void camera_mirror_mode_changed_cb(lv_event_t * event) {
    lv_obj_t * dropdown = lv_event_get_target(event);
    uint32_t index = lv_dropdown_get_selected(dropdown);
    if (index != app.settings.camera_mirror_mode) {
        app.settings.camera_mirror_mode = index;
    }
}

static void reversing_guide_lines_mode_changed_cb(lv_event_t * event) {
    lv_obj_t * dropdown = lv_event_get_target(event);
    uint32_t index = lv_dropdown_get_selected(dropdown);
    if (index != app.settings.reversing_guide_lines_mode) {
        app.settings.reversing_guide_lines_mode = index;
    }
}

static void guide_line_customization_cb(lv_event_t * event) {

}

static void suspension_key_mode_changed_cb(lv_event_t * event) {
    lv_obj_t * dropdown = lv_event_get_target(event);
    uint32_t index = lv_dropdown_get_selected(dropdown);
    if (index != app.settings.suspension_key_mode) {
        app.settings.suspension_key_mode = index;
    }
}

static void driving_mode_changed_cb(lv_event_t * event) {
    lv_obj_t * dropdown = lv_event_get_target(event);
    uint32_t index = lv_dropdown_get_selected(dropdown);
    if (index != app.settings.driving_mode) {
        app.settings.driving_mode = index;
    }
}

static lv_obj_t * make_container(lv_obj_t * parent) {
    lv_obj_t * root = lv_obj_create(parent);
    lv_obj_set_size(root, LV_PCT(100), LV_SIZE_CONTENT);
    lv_obj_set_style_pad_row(root, 0, LV_PART_MAIN);
    lv_obj_set_style_border_width(root, 0, LV_PART_MAIN);
    lv_obj_set_style_outline_width(root, 0, LV_PART_MAIN);
    return root;
}

static void fragment_constructor_ctor(lv_fragment_t * self, void * args) {

}

static lv_obj_t * fragment_create_obj(lv_fragment_t * fragment, lv_obj_t * container) {
    lv_obj_t * root = lv_obj_create(container);
    lv_obj_set_style_pad_row(root, LV_DPX(6), LV_PART_MAIN);
    lv_obj_set_size(root, LV_PCT(100), LV_PCT(100));
    lv_obj_set_layout(root, LV_LAYOUT_FLEX);
    lv_obj_set_flex_flow(root, LV_FLEX_FLOW_COLUMN);

    //#1
    lv_obj_t * contain = make_container(root);
    lv_obj_t * tsc_button = lv_button_create(contain);
    lv_obj_set_width(tsc_button, LV_PCT(100));
    lv_obj_t * tsc_label = lv_label_create(tsc_button);
    lv_obj_set_align(tsc_label, LV_ALIGN_CENTER);
    lv_label_set_text(tsc_label, _("Touch Screen Calibration"));
    lv_obj_add_event_cb(tsc_button, screen_clibration_cb, LV_EVENT_CLICKED, 0);

    //#2
    contain = make_container(root);
    lv_obj_t * rfs_button = lv_button_create(contain);
    lv_obj_set_width(rfs_button, LV_PCT(100));
    lv_obj_t * rfs_label = lv_label_create(rfs_button);
    lv_obj_set_align(rfs_label, LV_ALIGN_CENTER);
    lv_label_set_text(rfs_label, _("Restore Factory Settings"));
    lv_obj_add_event_cb(rfs_label, restore_factory_settings_cb, LV_EVENT_CLICKED, 0);

    //#3
    contain = make_container(root);
    lv_obj_t * lang_label = lv_label_create(contain);
    lv_obj_set_align(lang_label, LV_ALIGN_LEFT_MID);
    lv_label_set_text(lang_label, _("Language"));
    lv_obj_t * lang_dropdown = lv_dropdown_create(contain);
    lv_obj_set_size(lang_dropdown, LV_PCT(30), LV_SIZE_CONTENT);
    lv_dropdown_clear_options(lang_dropdown);
    lv_obj_set_align(lang_dropdown, LV_ALIGN_RIGHT_MID);
    for (uint32_t i = 0; i < sizeof(app.support_language)/sizeof(app.support_language[0]); i++) {
        lv_dropdown_add_option(lang_dropdown, app.support_language[i].name, i);
    }
    lv_dropdown_set_selected(lang_dropdown, app.settings.language_index);
    lv_obj_add_event_cb(lang_dropdown, language_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    //#4
    contain = make_container(root);
    lv_obj_t * font_label = lv_label_create(contain);
    lv_obj_set_align(font_label, LV_ALIGN_LEFT_MID);
    lv_label_set_text(font_label, _("Font"));
    lv_obj_t * font_dropdown = lv_dropdown_create(contain);
    lv_obj_set_size(font_dropdown, LV_PCT(30), LV_SIZE_CONTENT);
    lv_dropdown_clear_options(font_dropdown);
    lv_obj_set_align(font_dropdown, LV_ALIGN_RIGHT_MID);
    lv_dropdown_set_options_static(font_dropdown, "14\n16");
    lv_dropdown_set_selected(font_dropdown, app.settings.font_index);
    lv_obj_add_event_cb(font_dropdown, font_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    //#5
    contain = make_container(root);
    lv_obj_t * cms_label = lv_label_create(contain);
    lv_obj_set_align(cms_label, LV_ALIGN_LEFT_MID);
    lv_label_set_text(cms_label, _("Camera Mirroring Settings"));
    lv_obj_t * cms_dropdown = lv_dropdown_create(contain);
    lv_obj_set_size(cms_dropdown, LV_PCT(30), LV_SIZE_CONTENT);
    lv_dropdown_clear_options(cms_dropdown);
    lv_obj_set_align(cms_dropdown, LV_ALIGN_RIGHT_MID);
    lv_dropdown_add_option(cms_dropdown, _("Vertical"), 0);
    lv_dropdown_add_option(cms_dropdown, _("Horizontal"), 1);
    lv_dropdown_add_option(cms_dropdown, _("Fully"), 2);
    lv_dropdown_add_option(cms_dropdown, _("Off"), 3);
    lv_dropdown_set_selected(lang_dropdown, app.settings.camera_mirror_mode);
    lv_obj_add_event_cb(lang_dropdown, camera_mirror_mode_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    //#6
    contain = make_container(root);
    lv_obj_t * rgls_label = lv_label_create(contain);
    lv_obj_set_align(rgls_label, LV_ALIGN_LEFT_MID);
    lv_label_set_text(rgls_label, _("Reversing Guide Lines Setting"));
    lv_obj_t * rgls_dropdown = lv_dropdown_create(contain);
    lv_obj_set_size(rgls_dropdown, LV_PCT(30), LV_SIZE_CONTENT);
    lv_dropdown_clear_options(rgls_dropdown);
    lv_obj_set_align(rgls_dropdown, LV_ALIGN_RIGHT_MID);
    lv_dropdown_add_option(rgls_dropdown, _("Off"), 0);
    lv_dropdown_add_option(rgls_dropdown, _("On"), 1);
    lv_dropdown_set_selected(rgls_dropdown, app.settings.reversing_guide_lines_mode);
    lv_obj_add_event_cb(rgls_dropdown, reversing_guide_lines_mode_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    //#7
    contain = make_container(root);
    lv_obj_t * glc_button = lv_button_create(contain);
    lv_obj_set_width(glc_button, LV_PCT(100));
    lv_obj_t * glc_label = lv_label_create(glc_button);
    lv_obj_set_align(glc_label, LV_ALIGN_CENTER);
    lv_label_set_text(glc_label, _("Touch Screen Calibration"));
    lv_obj_add_event_cb(glc_button, guide_line_customization_cb, LV_EVENT_CLICKED, NULL);

    //#8
    contain = make_container(root);
    lv_obj_t * sks_label = lv_label_create(contain);
    lv_obj_set_align(sks_label, LV_ALIGN_LEFT_MID);
    lv_label_set_text(sks_label, _("Suspension key switch"));
    lv_obj_t * sks_dropdown = lv_dropdown_create(contain);
    lv_obj_set_size(sks_dropdown, LV_PCT(30), LV_SIZE_CONTENT);
    lv_dropdown_clear_options(sks_dropdown);
    lv_obj_set_align(sks_dropdown, LV_ALIGN_RIGHT_MID);
    lv_dropdown_add_option(sks_dropdown, _("Off"), 0);
    lv_dropdown_add_option(sks_dropdown, _("On"), 1);
    lv_dropdown_set_selected(sks_dropdown, app.settings.suspension_key_mode);
    lv_obj_add_event_cb(sks_dropdown, suspension_key_mode_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    //#8
    contain = make_container(root);
    lv_obj_t * dm_label = lv_label_create(contain);
    lv_obj_set_align(dm_label, LV_ALIGN_LEFT_MID);
    lv_label_set_text(dm_label, _("Driving Mode"));
    lv_obj_t * dm_dropdown = lv_dropdown_create(contain);
    lv_obj_set_size(dm_dropdown, LV_PCT(30), LV_SIZE_CONTENT);
    lv_dropdown_clear_options(dm_dropdown);
    lv_obj_set_align(dm_dropdown, LV_ALIGN_RIGHT_MID);
    lv_dropdown_add_option(dm_dropdown, _("Left"), 0);
    lv_dropdown_add_option(dm_dropdown, _("Right"), 1);
    lv_dropdown_set_selected(sks_dropdown, app.settings.driving_mode);
    lv_obj_add_event_cb(sks_dropdown, suspension_key_mode_changed_cb, LV_EVENT_VALUE_CHANGED, NULL);

    return root;
}

static bool fragment_event_cb(lv_fragment_t * self, int code, void * userdata) {
    if (code == 0) {
        lv_label_set_text(lv_obj_get_child(self->obj, 0), _("Touch Screen Calibration"));
        lv_label_set_text(lv_obj_get_child(self->obj, 1), _("Touch Screen Calibration"));
        return false;
    }

    return true;
}

lv_fragment_class_t setting_basic_fragment_cls = {
    .constructor_cb = fragment_constructor_ctor,
    .create_obj_cb = fragment_create_obj,
    .event_cb = fragment_event_cb,
    .instance_size = sizeof(lv_fragment_t)
};